package org.example.java8.base64;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
//import java.util.UUID;

public class Test {

	public static void main(String[] args) {
	    final String txt = "我是一个打工人";

	    String encodeStr = 
	    Base64.getEncoder().encodeToString(txt.getBytes(StandardCharsets.UTF_8));
	    System.out.println(encodeStr);
	    
	    String decodStr = 
	    new String(Base64.getDecoder().decode(encodeStr), StandardCharsets.UTF_8);
	    System.out.println(decodStr);
	    
	    String encodeMimeStr = 
	    Base64.getMimeEncoder().encodeToString(txt.getBytes(StandardCharsets.UTF_8));
	    System.out.println(encodeMimeStr);
	    
	    String decodeMimeStr = 
	    new String(Base64.getMimeDecoder().decode(encodeMimeStr), StandardCharsets.UTF_8);
	    System.out.println(decodeMimeStr);
	    
	    
	    
	    String encodeUrlStr = 
	    Base64.getUrlEncoder().encodeToString(txt.getBytes(StandardCharsets.UTF_8));
	    System.out.println(encodeUrlStr);
	    
	    String decodUrlStr = 
	    new String(Base64.getUrlDecoder().decode(encodeUrlStr), StandardCharsets.UTF_8);
	    System.out.println(decodUrlStr);
		
//		test();
	}

	
	static void test() {
      try {
          
          // 使用基本编码
          String base64encodedString = Base64.getEncoder().encodeToString("java8".getBytes("utf-8"));
          System.out.println("Base64 编码字符串 (基本) :" + base64encodedString);
         
          // 解码
          byte[] base64decodedBytes = Base64.getDecoder().decode(base64encodedString);
         
          System.out.println("原始字符串: " + new String(base64decodedBytes, "utf-8"));
          base64encodedString = Base64.getUrlEncoder().encodeToString("java8".getBytes("utf-8"));
          System.out.println("Base64 编码字符串 (URL) :" + base64encodedString);
         
//	          StringBuilder stringBuilder = new StringBuilder();
//	         
//	          for (int i = 0; i < 10; ++i) {
//	             stringBuilder.append(UUID.randomUUID().toString());
//	          }
         
          byte[] mimeBytes = "java8".toString().getBytes("utf-8");
          String mimeEncodedString = Base64.getMimeEncoder().encodeToString(mimeBytes);
          System.out.println("Base64 编码字符串 (MIME) :" + mimeEncodedString);
          
       }catch(UnsupportedEncodingException e){
          System.out.println("Error :" + e.getMessage());
       }
	}
}
